import { GameMode, world } from "@minecraft/server";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { PixelPartyDaemon } from "../components/daemon";
import { PixelPartyPatternManager } from "../components/patternManager";
import { PixelPartyPlayerView } from "../components/playerView";
import { pixelPartyModule } from "../module";
import { PixelPartyEndState } from "./endState";
import { InfoScoreboard } from "@sapi-game/gameComponent/view/infoScoreboard";
import { getFormattedTime, isReferee } from "@/func";
import { PixelPartyRelife } from "../components/relife";
import { PixelPartyItemManager } from "../components/item";

export class PixelPartyMainState extends pixelPartyModule.State {
    private readonly initTime = 15;

    override onEnter(): void {
        this.init();
        this.addComponent(Timer, {
            initialTime: this.initTime,
        })
            .addComponent(PixelPartyPatternManager)
            .addComponent(PixelPartyPlayerView)
            .addComponent(PixelPartyDaemon)
            .addComponent(PixelPartyRelife)
            .addComponent(PixelPartyItemManager)
            .addComponent(InfoScoreboard, {
                scoreBoardName: "PixelPartyInfo",
                displayName: "§a§l色盲派对",
                showOnAttach: true,
                paddingLeft: 2,
                header: () => ["§7" + getFormattedTime()],
                footer: () => ["§d§lPartyGames"],
            });

        this.timeActions();
        this.getComponent(Timer).start(); //开始计时
    }

    init() {
        //重置结构
        this.context.map.defaultStructure.place();
        world.gameRules.pvp = false;
        this.context.players.forEach((p) => {
            p.player.teleport(this.context.map.spawnPoint);
            p.player.setGameMode(GameMode.Adventure);
            p.clear();
            p.runCommand("effect @s clear");
            if (isReferee(p.player)) {
                p.relifeTimes++;
                p.sendMessage("你获得了一次复活次数");
            }
        });
    }

    timeActions() {
        const timer = this.getComponent(Timer);
        const patternManager = this.getComponent(PixelPartyPatternManager);
        this.subscribe(timer.events.tick, (t) => {
            switch (t.remainingTime) {
                case 13:
                    //开始生成(原图)
                    patternManager.genPattern();
                    break;
                case 10:
                    this.context.round++;
                    if (this.context.round == 5) {
                        this.enablePvP();
                    }
                    //放进游戏
                    patternManager.setBlocks();
                    this.getComponent(PixelPartyRelife).relifeAll();
                    this.getComponent(PixelPartyItemManager).loadItems(2);
                    this.context.players.forEach((p) => p.clear()); //清除方块
                    break;
                case 5:
                    //给方块
                    patternManager.updateRetainColor();
                    const block = patternManager.getRetainBlock();
                    this.getComponent(PixelPartyPlayerView).showColorBlock(
                        block.id
                    );
                    break;
                case 0:
                    //消失
                    patternManager.removeBlocks();
                    this.getComponent(PixelPartyItemManager).rmItems();
                    timer.set(this.initTime);
                    break;
            }
        });
    }

    enablePvP() {
        world.gameRules.pvp = true;
        this.context.players.title("§4§lPVP已开启");
        this.context.players.playSound("mob.wither.break_block", {
            volume: 0.2,
        });
    }

    end() {
        this.context.winner = this.context.players.find(
            (p) => p.isValid && p.alive
        );
        this.transitionTo(PixelPartyEndState);
    }
}
